//
//  PhotoDataModel.m
//  WOFishingBoat
//
//  Created by 郑逸凯 on 2019/1/7.
//  Copyright © 2019年 郑逸凯. All rights reserved.
//

#import "PhotoDataModel.h"

@implementation PhotoDataModel

- (void)generatePhotoSectionByAppendArray:(NSArray *)photoArray {
    if (photoArray.count  == 0) {
        return;
    }
    if (self.photoArray == nil) {
        self.photoArray = [NSMutableArray array];
    }
    if (self.photoSectionModels == nil) {
        self.photoSectionModels = [NSMutableArray array];
    }
    [self.photoArray addObjectsFromArray:photoArray];
    PhotoSectionModel *lastPhotoSection = [self.photoSectionModels lastObject];
    for (PhotoModel *model in photoArray) {
        if (lastPhotoSection && [lastPhotoSection.sectionTime isEqualToString:model.sectionTime]) {
            //如果分组存在且时间相等，则将photo加入该组
            [lastPhotoSection.photoArray addObject:model];
        }else {
            lastPhotoSection  = [[PhotoSectionModel alloc] init];
            lastPhotoSection.sectionTime = model.sectionTime;
            [self.photoSectionModels addObject:lastPhotoSection];
            [lastPhotoSection.photoArray addObject:model];
        }
    }
    
}

- (void)generatePhotoSectionByCurrentPhotoArray {
    self.photoSectionModels = nil;
    PhotoSectionModel *lastPhotoSection = nil;
    for (PhotoModel *model in self.photoArray) {
        if (lastPhotoSection && [lastPhotoSection.sectionTime isEqualToString:model.sectionTime]) {
            //如果分组存在且时间相等，则将photo加入该组
            [lastPhotoSection.photoArray addObject:model];
        }else {
            lastPhotoSection  = [[PhotoSectionModel alloc] init];
            lastPhotoSection.sectionTime = model.sectionTime;
            [self.photoSectionModels addObject:lastPhotoSection];
            [lastPhotoSection.photoArray addObject:model];
        }
    }
}

- (NSArray *)generatsSelectedPhotoArray {
    NSMutableArray *array = [NSMutableArray array];
    for (PhotoModel *model in self.photoArray) {
        if (model.isSelected) {
            [array addObject:model];
        }
    }
    return array;
}

- (NSInteger)indexAtTotalWithSection:(NSInteger)section row:(NSInteger)row {
    NSInteger index = 0;
    for (NSInteger i = 0; i < self.photoSectionModels.count; i++) {
        if (section == i) {
            index += row;
            break;
        }else {
            PhotoSectionModel *photoSectionModel = self.photoSectionModels[i];
            index += photoSectionModel.photoArray.count;

        }
    }
    return index;
}

- (NSMutableArray<PhotoModel *> *)photoArray {
    if (!_photoArray) {
        _photoArray = [NSMutableArray array];
    }
    return _photoArray;
}

- (NSMutableArray<PhotoSectionModel *> *)photoSectionModels {
    if (!_photoSectionModels) {
        _photoSectionModels = [NSMutableArray array];
    }
    return _photoSectionModels;
}

@end
